package com.smartHire.controller.User.Search;

import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Goods;
import com.smartHire.domain.Shop;
import com.smartHire.service.GoodsService;
import com.smartHire.service.ShopService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "SearchShopServlet", value = "/SearchShopServlet")
public class SearchShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            ShopService shopService = (ShopService) ctx.getBean("shopService");

            Shop shop = new Shop();
            shop.setShop_name(name);
            List<Shop> shops = shopService.searchShop(shop);

            response.setCharacterEncoding("utf-8");
            String jsonStr = JSONObject.toJSONString(shops);
            response.getWriter().write(jsonStr);

        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
